clear all
set more off

cap log close
log using $DIR\cr_capital_stock, t replace
*** Investiagate capital data

use $ENIADIR\enia, clear
rename id_* *
rename ano year
global capvars abaf acthab cbn* cbu* cor* edidep vbu* maqdep muedep otrdep ref* ///
       saldeb saledi salhab salmaq salter salveh tdeprec vbn* vbu* vehdep voc* vstk
keep id year $capvars

*** Convert from thousands pesos to million pesos
foreach var of varlist $capvars {
  qui replace `var' = `var'/1000
  qui replace `var' = . if `var'==0
  }

/*tabstat abaf acthab tdeprec vstk,by(year) stat(n)
tabstat sal*,by(year) stat(n)
tabstat cb*,by(year) stat(n)
tabstat vb*,by(year) stat(n)
tabstat *dep,by(year) stat(n)
*/

foreach var of varlist $capvars {
  qui replace `var' = 0 if `var'==.
  }

tabstat vstk,by(year) stat(n mean median iqr)  
replace vstk = saledi + salmaq + salter + salveh if vstk==0
tabstat vstk,by(year) stat(n mean median iqr)  

*** Construct capital stock
rename vstk capstk
sort id year
gen first = id~=id[_n-1] | year~=year[_n-1]+1
egen cap_exp = rowtotal(cb*)
gen deprec = edidep + maqdep + muedep + vehdep
gen cap_sale = vbuedi + vbumaq + vbuter + vbuveh
corr deprec tdeprec 

foreach var of varlist cap_exp deprec cap_sale {
  tabstat `var',by(year) stat(n mean median iqr)
}

gen cap_const = capstk if first
replace cap_const = cap_const[_n-1] + cap_exp - cap_sale - deprec if ~first

*** Recorded capital stock and constructed capital stock are correlated, but
*** not perfectly. Constructed capital can be negative when there is a big jump 
*** not captured by data.
gen loga = log(capstk)
gen logac = log(cap_const)
scatter logac loga
corr logac loga
sum logac loga
gen diff = (logac-loga)/((logac+loga)/2)
sum diff,det

foreach x in a ac {
  qui gen dlog`x' = log`x'-log`x'[_n-1] if ~first
  qui gen ddlog`x' = dlog`x'-dlog`x'[_n-1] if ~first[_n-1] & ~first
  sum ddlog`x',det
  qui gen error`x' = abs(ddlog`x')>2 & ddlog`x' ~=.
}
*scatter ddlog*
tab error*

*** Correct capital stock using constructed stock when there are large jumps
*** of opposite sign
replace capstk = cap_const if errora[_n+1] & ~errorac[_n+1] & cap_const>0
replace capstk = cap_const if errora & ~errorac & cap_const>0
gen cap_corr1 = (errora[_n-1] & ~errorac[_n-1] & cap_const>0) ///
              | (errora & ~errorac & cap_const>0)
			  
*** Check results
drop ddlog* dlog* error*
replace loga = log(capstk)
foreach x in a ac {
  qui gen dlog`x' = log`x'-log`x'[_n-1] if ~first
  qui gen ddlog`x' = dlog`x'-dlog`x'[_n-1] if ~first[_n-1] & ~first
  sum ddlog`x',det
  qui gen error`x' = abs(ddlog`x')>2 & ddlog`x' ~=.
}
*scatter ddlog*
tab error*

*** Repeat one more time
*** Correct capital stock using constructed stock when there are large jumps
*** of opposite sign
replace capstk = cap_const if errora[_n+1] & ~errorac[_n+1] & cap_const>0
replace capstk = cap_const if errora & ~errorac & cap_const>0
gen cap_corr2 = (errora[_n-1] & ~errorac[_n-1] & cap_const>0) ///
              | (errora & ~errorac & cap_const>0)

*** Check results
drop ddlog* dlog* error*
replace loga = log(capstk)
foreach x in a ac {
  qui gen dlog`x' = log`x'-log`x'[_n-1] if ~first
  qui gen ddlog`x' = dlog`x'-dlog`x'[_n-1] if ~first[_n-1] & ~first
  sum ddlog`x',det
  qui gen error`x' = abs(ddlog`x')>2 & ddlog`x' ~=.
}
*scatter ddlog*
tab error*
tab cap_corr1 cap_corr2
gen cap_correct = cap_corr1 | cap_corr2

*** Count firms with capital errors
egen x = sum(errora),by(id)
gen cap_drop = x>0
drop x
tab year cap_drop
count if first & cap_drop
scatter ddlog* if ~cap_drop

export excel id year errora dloga ddloga capstk cap_const dlogac ddlogac errorac ///
       if errora | (errora[_n+1] & id==id[_n+1]) | (errora[_n+2] & id==[_n+2])   ///
	   using $ENIADIR\cap_error, first(var) replace

rename errora cap_error
order id year errora dloga ddloga capstk cap_const dlogac ddlogac errorac cap_error

keep id year cap_correct cap_drop capstk cap_error
label data "Capital stock corrected for large changes where possible"
label var cap_correct "D (capital replaced by constructed varsion)"
label var cap_drop "D (firm has capital stock errors)"
label var cap_error "D (obs has capital stock errors)"
label var capstk "Corrected capital stock"
do $DIR\cleanfile
save $ENIADIR\corrected_capital, replace

log close
